package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingPlanProjectMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 护理计划Service实现类
 */
@Service
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingPlanProjectMapper nursingPlanProjectMapper;

    // 查询所有护理计划
    @Override
    public List<NursingPlanVo> getAll() {
        List<NursingPlan> nursingPlans = nursingPlanMapper.getAll();
        List<NursingPlanVo> nursingPlanVos = new ArrayList();
        for (NursingPlan nursingPlan : nursingPlans) {
            NursingPlanVo nv = new NursingPlanVo();
            BeanUtils.copyProperties(nursingPlan, nv);
            nursingPlanVos.add(nv);
        }
        return nursingPlanVos;
    }

    // 新增护理计划

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void add(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        // 设置状态为禁用
        nursingPlan.setStatus(0);
        nursingPlanMapper.add(nursingPlan);

        nursingPlanDto.getProjectPlans().forEach(projectPlan -> projectPlan.setPlanId(nursingPlan.getId()));
        if (nursingPlanDto.getProjectPlans() != null && nursingPlanDto.getProjectPlans().size() > 0) {
            nursingPlanProjectMapper.add(nursingPlanDto.getProjectPlans());
        }
    }

    // 查询护理计划
    @Override
    public PageResponse<NursingPlanVo> getPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingPlanVo> nursingPlans = nursingPlanMapper.getPage(name, status);
        PageResponse<NursingPlanVo> pageResponse = new PageResponse(nursingPlans);
        pageResponse.setRecords(nursingPlans.getResult());

        return pageResponse;
    }

    // 根据id查询护理计划
    @Override
    public NursingPlanVo getById(Integer id) {
        NursingPlan nursingPlan = nursingPlanMapper.getById(id);

        return BeanUtil.toBean(nursingPlan, NursingPlanVo.class);
    }

    // 修改护理计划
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(NursingPlanDto nursingPlanDto, Long id) {

        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        nursingPlanMapper.update(nursingPlan);
        nursingPlanProjectMapper.delete(id);
        nursingPlanDto.getProjectPlans().forEach(projectPlan -> projectPlan.setPlanId(id));
        if (nursingPlanDto.getProjectPlans() != null && nursingPlanDto.getProjectPlans().size() > 0) {
            nursingPlanProjectMapper.add(nursingPlanDto.getProjectPlans());
        }
    }

    // 修改护理计划状态
    @Override
    public void updateStatus(Long id, Integer status) {
        NursingPlan nursingPlan = new NursingPlan();
        nursingPlan.setStatus(status);
        nursingPlan.setId(id);
        nursingPlanMapper.update(nursingPlan);
    }

    // 删除护理计划
    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void delete(Long id) {
        nursingPlanMapper.delete(id);
        nursingPlanProjectMapper.delete(id);

    }
}